#! /bin/bash

# --------------------------------------------------
# Project monitor:
#       https://github.com/Statemood/monitor
#
#
# --------------------------------------------------

# Usage:
#   check_http --host 11 --ip 22 --port 33 --item <arguments for curl -w> --url <optional>


curl=/usr/local/curl/bin/curl
test -x $curl || curl=curl

port=80
opts="-IL --max-time 5 -o /dev/null -s"

if [ "$#" -lt 4 ]
then
    echo 100001
    exit 1
fi
for arg in "$@"
do
    case $arg in
        --host)
            host="$2"
            shift 2
            ;;
        --ip)
            ip="$2"
            shift 2
            ;;
        --port)
            port="$2"
            shift 2
            ;;
        --item)
            item="$2"
            shift 2
            ;;
        --url)
            url="$2"
            ;;
    esac
done

test $port   = 443   && protocol=https || protocol=http
test "$host" = "$ip" && port_opt=":$port"
test "$ip"   = "0"   || resolve="--resolve $host:$port:$ip"

$curl $resolve $protocol://$host${port_opt}${url} $opts -w %{$item}
